

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>mindspore.communication &mdash; MindSpore master documentation</title>
  

  
  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />

  
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
        <script src="../_static/jquery.js"></script>
        <script src="../_static/underscore.js"></script>
        <script src="../_static/doctools.js"></script>
        <script src="../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="mindspore.compression" href="mindspore.compression.html" />
    <link rel="prev" title="mindspore.common.initializer" href="mindspore.common.initializer.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../index.html" class="icon icon-home"> MindSpore
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">MindSpore Python API</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="mindspore.html">mindspore</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.common.initializer.html">mindspore.common.initializer</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">mindspore.communication</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.compression.html">mindspore.compression</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.context.html">mindspore.context</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.dataset.html">mindspore.dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.dataset.audio.html">mindspore.dataset.audio</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.dataset.config.html">mindspore.dataset.config</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.dataset.text.html">mindspore.dataset.text</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.dataset.transforms.html">mindspore.dataset.transforms</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.dataset.vision.html">mindspore.dataset.vision</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.mindrecord.html">mindspore.mindrecord</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.nn.html">mindspore.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.nn.probability.html">mindspore.nn.probability</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.nn.transformer.html">mindspore.nn.transformer</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.numpy.html">mindspore.numpy</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.ops.html">mindspore.ops</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.parallel.html">mindspore.parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.parallel.nn.html">mindspore.parallel.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.profiler.html">mindspore.profiler</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.scipy.html">mindspore.scipy</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.train.html">mindspore.train</a></li>
<li class="toctree-l1"><a class="reference internal" href="mindspore.boost.html">mindspore.boost</a></li>
</ul>
<p class="caption"><span class="caption-text">MindSpore C++ API</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://www.mindspore.cn/lite/api/zh-CN/master/api_cpp/mindspore.html">MindSpore Lite↗</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">MindSpore</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
        
      <li>mindspore.communication</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
          
            <a href="../_sources/api_python/mindspore.communication.rst.txt" rel="nofollow"> View page source</a>
          
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="mindspore-communication">
<h1>mindspore.communication<a class="headerlink" href="#mindspore-communication" title="Permalink to this headline">¶</a></h1>
<p>集合通信接口。</p>
<p>注意，集合通信接口需要预先设置环境变量。对于Ascend，用户需要配置rank_table，设置rank_id和device_id，相关教程可参考：
<a class="reference external" href="https://www.mindspore.cn/tutorials/zh-CN/master/intermediate/distributed_training/distributed_training_ascend.html">https://www.mindspore.cn/tutorials/zh-CN/master/intermediate/distributed_training/distributed_training_ascend.html</a>。
对于GPU，用户需要预先配置host_file以及mpi，相关教程参考：
<a class="reference external" href="https://www.mindspore.cn/tutorials/zh-CN/master/intermediate/distributed_training/distributed_training_gpu.html">https://www.mindspore.cn/tutorials/zh-CN/master/intermediate/distributed_training/distributed_training_gpu.html</a>。</p>
<p>目前尚不支持CPU。</p>
<dl class="class">
<dt id="mindspore.communication.GlobalComm">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.communication.</code><code class="sig-name descname">GlobalComm</code><a class="headerlink" href="#mindspore.communication.GlobalComm" title="Permalink to this definition">¶</a></dt>
<dd><p>GlobalComm 是一个储存通信信息的全局类。 成员包含：BACKEND、WORLD_COMM_GROUP。</p>
<ul class="simple">
<li><p>BACKEND：使用的通信库，HCCL或者NCCL。</p></li>
<li><p>WORLD_COMM_GROUP：全局通信域。</p></li>
</ul>
</dd></dl>

<dl class="function">
<dt id="mindspore.communication.init">
<code class="sig-prename descclassname">mindspore.communication.</code><code class="sig-name descname">init</code><span class="sig-paren">(</span><em class="sig-param">backend_name=None</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.communication.init" title="Permalink to this definition">¶</a></dt>
<dd><p>初始化通信服务需要的分布式后端，例如‘HCCL’或‘NCCL’服务。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>backend_name</strong> (str) – 分布式后端的名称，可选HCCL或NCCL。如果未设置则根据硬件平台类型（device_target）进行推断，默认值为None。</p></li>
</ul>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>TypeError</strong> – 参数 <cite>backend_name</cite> 不是字符串。</p></li>
<li><p><strong>RuntimeError</strong> – 1）硬件设备类型无效；2）后台服务无效；3）分布式计算初始化失败；4）未设置环境变量 <cite>RANK_ID</cite> 或 <cite>MINDSPORE_HCCL_CONFIG_PATH</cite> 的情况下初始化HCCL服务。</p></li>
<li><p><strong>ValueError</strong> – 环境变量 <cite>RANK_ID</cite> 设置成非数字。</p></li>
</ul>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.communication</span> <span class="kn">import</span> <span class="n">init</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">init</span><span class="p">()</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="mindspore.communication.release">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.communication.</code><code class="sig-name descname">release</code><a class="headerlink" href="#mindspore.communication.release" title="Permalink to this definition">¶</a></dt>
<dd><p>释放分布式资源，例如‘HCCL’或‘NCCL’服务。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p><cite>release</cite> 方法应该在 <cite>init</cite> 方法之后使用。</p>
</div>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>RuntimeError</strong> - 在释放分布式资源失败时抛出。</p></li>
</ul>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.communication</span> <span class="kn">import</span> <span class="n">init</span><span class="p">,</span> <span class="n">release</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">init</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">release</span><span class="p">()</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="mindspore.communication.get_rank">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.communication.</code><code class="sig-name descname">get_rank</code><span class="sig-paren">(</span><em class="sig-param">group=GlobalComm.WORLD_COMM_GROUP</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.communication.get_rank" title="Permalink to this definition">¶</a></dt>
<dd><p>在指定通信组中获取当前的设备序号。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>group</strong> (str) - 通信组名称，通常由 <cite>create_group</cite> 方法创建，否则将使用默认组。默认值：‘GlobalComm.WORLD_COMM_GROUP’。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>int, 调用该方法的进程对应的组内序号。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>TypeError</strong> – 在参数 <cite>group</cite> 不是字符串时抛出。</p></li>
<li><p><strong>ValueError</strong> – 在后台不可用时抛出。</p></li>
<li><p><strong>RuntimeError</strong> – 在‘HCCL’或‘NCCL’服务不可用时抛出。</p></li>
</ul>
</dd></dl>

<dl class="class">
<dt id="mindspore.communication.get_group_size">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.communication.</code><code class="sig-name descname">get_group_size</code><span class="sig-paren">(</span><em class="sig-param">group=GlobalComm.WORLD_COMM_GROUP</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.communication.get_group_size" title="Permalink to this definition">¶</a></dt>
<dd><p>获取指定通信组实例的rank_size。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p><cite>get_group_size</cite> 方法应该在 <cite>init</cite> 方法之后使用。在跑用例之前用户需要预先配置通信相关的环境变量。</p>
</div>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>group</strong> (str) - 指定工作组实例（由 create_group 方法创建）的名称，支持数据类型为str，默认值为‘WORLD_COMM_GROUP’。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>指定通信组实例的rank_size，数据类型为int。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>TypeError</strong> – 在参数 <cite>group</cite> 不是字符串时抛出。</p></li>
<li><p><strong>ValueError</strong> – 在后台不可用时抛出。</p></li>
<li><p><strong>RuntimeError</strong> – 在‘HCCL’或‘NCCL’服务不可用时抛出。</p></li>
</ul>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.context</span> <span class="kn">import</span> <span class="n">set_context</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.communication.management</span> <span class="kn">import</span> <span class="n">init</span><span class="p">,</span> <span class="n">get_group_size</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">set_context</span><span class="p">(</span><span class="n">device_target</span><span class="o">=</span><span class="s2">&quot;Ascend&quot;</span><span class="p">,</span> <span class="n">device_num</span><span class="o">=</span><span class="mi">8</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">init</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">group_size</span> <span class="o">=</span> <span class="n">get_group_size</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="s2">&quot;group_size is: &quot;</span><span class="p">,</span> <span class="n">group_size</span><span class="p">)</span>
<span class="go">group_size is: 8</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="mindspore.communication.get_world_rank_from_group_rank">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.communication.</code><code class="sig-name descname">get_world_rank_from_group_rank</code><span class="sig-paren">(</span><em class="sig-param">group</em>, <em class="sig-param">group_rank_id</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.communication.get_world_rank_from_group_rank" title="Permalink to this definition">¶</a></dt>
<dd><p>由指定通信组中的设备序号获取通信集群中的全局设备序号。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<ul class="simple">
<li><p>GPU 版本的MindSpore不支持此方法；</p></li>
<li><p>参数 <cite>group</cite> 不能是 <cite>hccl_world_group</cite>；</p></li>
<li><p><cite>get_world_rank_from_group_rank</cite> 方法应该在 <cite>init</cite> 方法之后使用。</p></li>
</ul>
</div>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>group</strong> (str) - 传入的通信组名称，通常由 <cite>create_group</cite> 方法创建。</p></li>
<li><p><strong>group_rank_id</strong> (int) - 通信组内的设备序号。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>int, 通信集群中的全局设备序号。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>TypeError</strong> – 参数 <cite>group</cite> 不是字符串或参数 <cite>group_rank_id</cite> 不是数字。</p></li>
<li><p><strong>ValueError</strong> – 参数 <cite>group</cite> 是 <cite>hccl_world_group</cite> 或后台不可用。</p></li>
<li><p><strong>RuntimeError</strong> – ‘HCCL’或‘NCCL’服务不可用，以及使用CPU版本的MindSpore。</p></li>
</ul>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.context</span> <span class="kn">import</span> <span class="n">set_context</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.communication.management</span> <span class="kn">import</span> <span class="n">init</span><span class="p">,</span> <span class="n">create_group</span><span class="p">,</span> <span class="n">get_world_rank_from_group_rank</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">set_context</span><span class="p">(</span><span class="n">device_target</span><span class="o">=</span><span class="s2">&quot;Ascend&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">init</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">group</span> <span class="o">=</span> <span class="s2">&quot;0-4&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rank_ids</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">4</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">create_group</span><span class="p">(</span><span class="n">group</span><span class="p">,</span> <span class="n">rank_ids</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">world_rank_id</span> <span class="o">=</span> <span class="n">get_world_rank_from_group_rank</span><span class="p">(</span><span class="n">group</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="s2">&quot;world_rank_id is: &quot;</span><span class="p">,</span> <span class="n">world_rank_id</span><span class="p">)</span>
<span class="go">world_rank_id is: 4</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="mindspore.communication.get_group_rank_from_world_rank">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.communication.</code><code class="sig-name descname">get_group_rank_from_world_rank</code><span class="sig-paren">(</span><em class="sig-param">world_rank_id</em>, <em class="sig-param">group</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.communication.get_group_rank_from_world_rank" title="Permalink to this definition">¶</a></dt>
<dd><p>由通信集群中的全局设备序号获取指定用户通信组中的rank ID。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<ul class="simple">
<li><p>GPU 版本的MindSpore不支持此方法；</p></li>
<li><p>参数 <cite>group</cite> 不能是 <cite>hccl_world_group</cite>；</p></li>
<li><p><cite>get_group_rank_from_world_rank</cite> 方法应该在 <cite>init</cite> 方法之后使用。</p></li>
</ul>
</div>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>world_rank_id</strong> (<cite>int</cite>) - 通信集群内的全局rank ID。</p></li>
<li><p><strong>group</strong> (<cite>str</cite>) - 指定通信组实例（由 create_group 方法创建）的名称。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>当前通信组内的rank_ID，数据类型为int。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>TypeError</strong> – 在参数 <cite>group_rank_id</cite> 不是数字或参数 <cite>group</cite> 不是字符串时抛出。</p></li>
<li><p><strong>ValueError</strong> – 在参数 <cite>group</cite> 是 <cite>hccl_world_group</cite> 或后台不可用时抛出。</p></li>
<li><p><strong>RuntimeError</strong> – 在‘HCCL’或‘NCCL’服务不可用，以及使用GPU版本的MindSpore时抛出。</p></li>
</ul>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.context</span> <span class="kn">import</span> <span class="n">set_context</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.communication.management</span> <span class="kn">import</span> <span class="n">init</span><span class="p">,</span> <span class="n">create_group</span><span class="p">,</span> <span class="n">get_group_rank_from_world_rank</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">set_context</span><span class="p">(</span><span class="n">device_target</span><span class="o">=</span><span class="s2">&quot;Ascend&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">init</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">group</span> <span class="o">=</span> <span class="s2">&quot;0-4&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rank_ids</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">4</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">create_group</span><span class="p">(</span><span class="n">group</span><span class="p">,</span> <span class="n">rank_ids</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">group_rank_id</span> <span class="o">=</span> <span class="n">get_group_rank_from_world_rank</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="n">group</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="s2">&quot;group_rank_id is: &quot;</span><span class="p">,</span> <span class="n">group_rank_id</span><span class="p">)</span>
<span class="go">group_rank_id is: 1</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="mindspore.communication.create_group">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.communication.</code><code class="sig-name descname">create_group</code><span class="sig-paren">(</span><em class="sig-param">group</em>, <em class="sig-param">rank_ids</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.communication.create_group" title="Permalink to this definition">¶</a></dt>
<dd><p>创建用户自定义的通信组实例。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<ul class="simple">
<li><p>GPU 版本的MindSpore不支持此方法；</p></li>
<li><p>列表rank_ids的长度应大于1；</p></li>
<li><p>列表rank_ids内不能有重复数据；</p></li>
<li><p><cite>create_group</cite> 方法应该在 <cite>init</cite> 方法之后使用。</p></li>
</ul>
</div>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>group</strong> (str) - 输入用户自定义的通信组实例名称，支持数据类型为str。</p></li>
<li><p><strong>rank_ids</strong> (list) - 设备编号列表。</p></li>
</ul>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>TypeError</strong> – 参数 <cite>group_rank_id</cite> 不是数字或参数 <cite>group</cite> 不是字符串。</p></li>
<li><p><strong>ValueError</strong> – 列表rank_ids的长度小于1，或列表rank_ids内有重复数据，以及后台无效。</p></li>
<li><p><strong>RuntimeError</strong> – 在‘HCCL’或‘NCCL’ 服务不可用，以及使用CPU版本的MindSpore。</p></li>
</ul>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.context</span> <span class="kn">import</span> <span class="n">set_context</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.ops</span> <span class="kn">import</span> <span class="n">operations</span> <span class="k">as</span> <span class="n">ops</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.communication.management</span> <span class="kn">import</span> <span class="n">init</span><span class="p">,</span> <span class="n">create_group</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">set_context</span><span class="p">(</span><span class="n">device_target</span><span class="o">=</span><span class="s2">&quot;Ascend&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">init</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">group</span> <span class="o">=</span> <span class="s2">&quot;0-8&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rank_ids</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">8</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">create_group</span><span class="p">(</span><span class="n">group</span><span class="p">,</span> <span class="n">rank_ids</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">allreduce</span> <span class="o">=</span> <span class="n">ops</span><span class="o">.</span><span class="n">AllReduce</span><span class="p">(</span><span class="n">group</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="mindspore.communication.get_local_rank">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.communication.</code><code class="sig-name descname">get_local_rank</code><span class="sig-paren">(</span><em class="sig-param">group=GlobalComm.WORLD_COMM_GROUP</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.communication.get_local_rank" title="Permalink to this definition">¶</a></dt>
<dd><p>获取指定通信组中当前设备的本地设备序号。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<ul class="simple">
<li><p>GPU 版本的MindSpore不支持此方法；</p></li>
<li><p><cite>get_local_rank</cite> 方法应该在 <cite>init</cite> 方法之后使用。</p></li>
</ul>
</div>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>group</strong> (<cite>str</cite>) - 通信组名称，通常由 <cite>create_group</cite> 方法创建，否则将使用默认组名称。默认值：‘WORLD_COMM_GROUP’。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>int, 调用该方法的进程对应的通信组内本地设备序号。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>TypeError</strong> – 在参数 <cite>group</cite> 不是字符串时抛出。</p></li>
<li><p><strong>ValueError</strong> – 在后台不可用时抛出。</p></li>
<li><p><strong>RuntimeError</strong> – 在‘HCCL’或‘NCCL’服务不可用时抛出。</p></li>
</ul>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.context</span> <span class="kn">import</span> <span class="n">set_context</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.communication.management</span> <span class="kn">import</span> <span class="n">init</span><span class="p">,</span> <span class="n">get_rank</span><span class="p">,</span> <span class="n">get_local_rank</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">set_context</span><span class="p">(</span><span class="n">device_target</span><span class="o">=</span><span class="s2">&quot;Ascend&quot;</span><span class="p">,</span> <span class="n">device_num</span><span class="o">=</span><span class="mi">16</span><span class="p">)</span> <span class="c1"># 2 server, each server with 8 NPU.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">init</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">world_rank</span> <span class="o">=</span> <span class="n">get_rank</span><span class="p">()</span> <span class="c1"># rank_id is 9.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">local_rank</span> <span class="o">=</span> <span class="n">get_local_rank</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="s2">&quot;local_rank is: </span><span class="si">{}</span><span class="s2">, world_rank is </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">local_rank</span><span class="p">,</span> <span class="n">world_rank</span><span class="p">))</span>
<span class="go">local_rank is: 1, world_rank is 9</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="mindspore.communication.get_local_rank_size">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.communication.</code><code class="sig-name descname">get_local_rank_size</code><span class="sig-paren">(</span><em class="sig-param">group=GlobalComm.WORLD_COMM_GROUP</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.communication.get_local_rank_size" title="Permalink to this definition">¶</a></dt>
<dd><p>获取指定通信组的本地设备总数。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<ul class="simple">
<li><p>GPU 版本的MindSpore不支持此方法；</p></li>
<li><p><cite>get_local_rank_size</cite> 方法应该在 <cite>init</cite> 方法之后使用。</p></li>
</ul>
</div>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>group</strong> (str) - 传入的通信组名称，通常由 <cite>create_group</cite> 方法创建，或默认使用‘WORLD_COMM_GROUP’。</p></li>
</ul>
<p><strong>返回：</strong></p>
<p>int, 调用该方法的进程对应的通信组设备总数。</p>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>TypeError</strong> – 在参数 <cite>group</cite> 不是字符串时抛出。</p></li>
<li><p><strong>ValueError</strong> – 在后台不可用时抛出。</p></li>
<li><p><strong>RuntimeError</strong> – 在‘HCCL’或‘NCCL’服务不可用时抛出。</p></li>
</ul>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.context</span> <span class="kn">import</span> <span class="n">set_context</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore.communication.management</span> <span class="kn">import</span> <span class="n">init</span><span class="p">,</span> <span class="n">get_local_rank_size</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">set_context</span><span class="p">(</span><span class="n">device_target</span><span class="o">=</span><span class="s2">&quot;Ascend&quot;</span><span class="p">,</span> <span class="n">device_num</span><span class="o">=</span><span class="mi">16</span><span class="p">)</span> <span class="c1"># 2 server, each server with 8 NPU.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">init</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">local_rank_size</span> <span class="o">=</span> <span class="n">get_local_rank_size</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="s2">&quot;local_rank_size is: &quot;</span><span class="p">,</span> <span class="n">local_rank_size</span><span class="p">)</span>
<span class="go">local_rank_size is: 8</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="mindspore.communication.destroy_group">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.communication.</code><code class="sig-name descname">destroy_group</code><span class="sig-paren">(</span><em class="sig-param">group</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.communication.destroy_group" title="Permalink to this definition">¶</a></dt>
<dd><p>注销用户通信组。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<ul class="simple">
<li><p>GPU 版本的MindSpore不支持此方法；</p></li>
<li><p>参数 <cite>group</cite> 不能是 <cite>hccl_world_group</cite>；</p></li>
<li><p><cite>destroy_group</cite> 方法应该在 <cite>init</cite> 方法之后使用。</p></li>
</ul>
</div>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>group</strong> (str) - 被注销通信组实例（通常由 create_group 方法创建）的名称。</p></li>
</ul>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>TypeError</strong> – 在参数 <cite>group</cite> 不是字符串时抛出。</p></li>
<li><p><strong>ValueError</strong> – 在参数 <cite>group</cite> 是 <cite>hccl_world_group</cite> 或后台不可用时抛出。</p></li>
<li><p><strong>RuntimeError</strong> – 在‘HCCL’或‘NCCL’服务不可用时抛出。</p></li>
</ul>
</dd></dl>

</div>


           </div>
           
          </div>
          <footer>
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
        <a href="mindspore.compression.html" class="btn btn-neutral float-right" title="mindspore.compression" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
        <a href="mindspore.common.initializer.html" class="btn btn-neutral float-left" title="mindspore.common.initializer" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2021, MindSpore.

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>